package ru.resumeService.web.servlet;

import ru.resumeService.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class Authorization extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        doPost(request, response);
    }

    @Override

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        UserServiceImpl usersService = new UserServiceImpl();
        if (request.getSession(true).getAttribute("isLogin") == null) {      //если закрыта, то проверяем введенные логин и пароль
            String login = request.getParameter("login");
            String password = request.getParameter("password");
            if (usersService.isUserValid(login, password)) {
                request.getSession(true).setAttribute("isLogin", usersService.isUserValid(login, password));
                response.sendRedirect(request.getContextPath() + "/resumeList");

            } else {
                request.getSession(true).setAttribute("isLogin", false);
                response.sendRedirect(request.getContextPath() + "/resumeList");
            }
        } else {
            response.sendRedirect(request.getContextPath() + "/resumeList");
        }
    }
}
